<?php
/**
 * Enter description here...
 *
 * @package if
 * @subpackage module.helper.controllers
 * @since 1.0
 */
class IfHelperCleanController extends IHelperController {
	function doIndex() {
		if ($this->request()->isPost()) {
			$types = x("type");
			if (is_array($types)) {
				foreach ($types as $type) {
					$this->_clean($type);
				}
			}
			$this->closeWindow();
		}
		
	}
	
	private function _clean($type) {
		$found = false;
		if ($type == "cache") {
			$this->log("clean cache start");
			$found = true;
			$files = f(IF_APP_ROOT . "/cache")->lists();
			$count = 0;
			foreach ($files as $file) {
				$name = $file->name();
				if ($name{0} != ".") {
					$file->delete();
					$count ++;
				}
			}
			$this->log("clean cache ok. ({$count} files were deleted)");
		}
		if ($type == "log") {
			$this->log("clean log start");
			$found = true;
			$files = f(IF_APP_ROOT . "/logs")->lists();
			$count = 0;
			foreach ($files as $file) {
				$name = $file->name();
				if ($name{0} != ".") {
					$file->delete();
					$count ++;
				}
			}
			$this->log("clean log ok. ({$count} files were deleted)");
		}
		if ($type == "compile") {
			$found = true;
			if_clean_compiler_dir();
		}
		if ($type == "template") {
			$this->log("clean templates start");
			$found = true;
			$smartyView = new ISmartyView();
			$smartyView->init(o("@view.smarty"));
			$files = f($smartyView->opt("compile_dir"))->lists("*.php");
			$count = 0;
			foreach ($files as $file) {
				$name = $file->name();
				if ($name{0} != ".") {
					$file->delete();
					$count ++;
				}
			}
			$this->log("clean templates cache ok. ({$count} files were deleted)");
		}		
		if (!$found) {
			$this->output("Which directory would you like to clean?", 0);
		}
	}
}

?>